$(function() {

    const tuichu = document.querySelector('.tuichu')
    tuichu.addEventListener('click', () => {
        sessionStorage.clear()
        localStorage.clear()
        location.replace("http://127.0.0.1:3007/")
        localStorage.setItem('user', '学生')

    })

    // 获取班级名称
    $('.inClass').on(
        'click',
        function() {
            $('.studentcheck').hide()
            $('.studentcheck3').hide()
            $('.studentcheck2').hide()
            $('.search').hide()

            $('td>span').empty()
            let token = localStorage.getItem("token");
            token = JSON.parse(token);
            $.ajax({
                url: 'http://127.0.0.1:3007/my/instructor/getclass',
                type: 'GET',
                data: {
                    id: token[0].id
                },
                headers: {
                    Authorization: token[0].Authorization
                },
                success: function(data) {
                    // 把数据库获取的放到localstorage里
                    // console.log(data.data[0].ask);
                    console.log(data.data);
                    // 动态添加
                    $('#className').empty()
                    $.each(data.data, function(i, n) {
                        // console.log(n);
                        console.log(n.class_name);
                        $('#className').append('<option value="' + n.class_name + '">' + n.class_name + '</option>')
                    })



                    $('#className').on('click', function() {
                        const classname = $(this).val()
                        $('.whereClass').html(classname)
                        let token = localStorage.getItem("token");
                        token = JSON.parse(token);
                        $.ajax({
                            url: 'http://127.0.0.1:3007/my/instructor/classView',
                            type: 'POST',
                            data: {
                                id: token[0].id,
                                classname
                            },
                            headers: {
                                Authorization: token[0].Authorization
                            },
                            success: function(data) {
                                // 把数据库获取的放到localstorage里
                                // console.log(data.data[0].ask);
                                let turnPeople = 0
                                let leave = 0
                                    // if (data.data[i].attendendance.data[0] === 1) {
                                    //     turnPeople++
                                    // }
                                    // console.log(data.data[i]);
                                for (let i = 0; i < data.data.length; i++) {
                                    // console.log(data.data[i]);
                                    if (data.data[i].attendendance.data[0] === 1) {
                                        turnPeople++
                                    } else if (data.data[i].leave.data[0] === 1) {
                                        leave++
                                    }
                                }
                                $('.turnPeople').html('出勤:' + turnPeople)
                                $('.leavePeople').html('请假:' + leave)
                                $('.allPeople').html('总共:' + data.data.length)
                                $('.studentcheck').show()

                            }
                        })
                    })



                }
            })
        }
    )




    // 按专业查询
    $('.inMajor').on('click', function() {
        $('.studentcheck').hide()
        $('.studentcheck2').hide()
        $('.studentcheck3').hide()
        $('.search').hide()
        $('td>span').empty()
        let token = localStorage.getItem("token");
        token = JSON.parse(token);
        $.ajax({
            url: 'http://127.0.0.1:3007/my/instructor/getmajor',
            type: 'GET',
            data: {
                id: token[0].id
            },
            headers: {
                Authorization: token[0].Authorization
            },
            success: function(data) {
                // 把数据库获取的放到localstorage里
                // console.log(data.data[0].ask);
                console.log(data.data);
                // 动态添加
                $('#className2').empty()
                $.each(data.data, function(i, n) {
                    // console.log(n);

                    $('#className2').append('<option value="' + n.major_name + '">' + n.major_name + '</option>')
                })


                $('#className2').on('click', function() {
                    const majorname = $(this).val()
                    $('.whereClass2').html(majorname)
                    let token = localStorage.getItem("token");
                    token = JSON.parse(token);
                    $.ajax({
                        url: 'http://127.0.0.1:3007/my/instructor/majorView',
                        type: 'POST',
                        data: {
                            id: token[0].id,
                            majorname
                        },
                        headers: {
                            Authorization: token[0].Authorization
                        },
                        success: function(data) {
                            console.log(data);
                            // 把数据库获取的放到localstorage里
                            // console.log(data.data[0].ask);
                            let turnPeople = 0
                            let leave = 0
                                // if (data.data[i].attendendance.data[0] === 1) {
                                //     turnPeople++
                                // }
                            for (let i = 0; i < data.data.length; i++) {
                                // console.log(data.data[i]);
                                if (data.data[i].attendendance.data[0] === 1) {
                                    turnPeople++
                                } else if (data.data[i].leave.data[0] === 1) {
                                    leave++
                                }
                            }
                            $('.turnPeople2').html('出勤:' + turnPeople)
                            $('.leavePeople2').html('请假:' + leave)
                            $('.allPeople2').html('总共:' + data.data.length)
                            $('.studentcheck2').show()

                        }
                    })
                })


            }
        })
    })

    $('.inStudent_id').on('click', function() {
            $('.search').show()
            $('.studentcheck3').hide()
        })
        // 按学号查询
    $('.inid').on('keydown', function(e) {
        if (e.keyCode === 13) {
            $('.studentcheck').hide()
            $('.studentcheck2').hide()
            $('.studentcheck3').hide()
                // console.log(inId.value);
                // console.log(this.value);
            let token = localStorage.getItem("token");
            token = JSON.parse(token);
            $.ajax({
                url: 'http://127.0.0.1:3007/my/instructor/getAttendance',
                type: 'POST',
                data: {
                    id: token[0].id,
                    student_id: this.value
                },
                headers: {
                    Authorization: token[0].Authorization
                },
                success: function(data) {
                    // 把数据库获取的放到localstorage里
                    let turnPeople = 0
                    let leave = 0

                    if (data.data[0].attendendance.data[0] === 1) {
                        turnPeople++
                    } else if (data.data[0].leave.data[0] === 1) {
                        leave++
                    }
                    // console.log(turnPeople, leave);
                    console.log(data.data[0]);
                    $('.turnPeople3').html('出勤:' + turnPeople)
                    $('.leavePeople3').html('请假:' + leave)
                    $('.whereClass3').html('姓名：' + data.data[0].name)
                    $('.classname').html('班级:' + data.data[0].class)
                    $('.studentcheck3').show()
                },

            })
        }
    })


})